//---------------------------------------------------------------------------

#include <vcl.h>
#include "EIC.h"
#pragma hdrstop

#include "CEditUpDown.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé est utilisé pour être sûr que les composants créés n'ont
// aucune fonction virtuelle pure.
//
static inline void ValidCtrCheck(CEditUpDown *)
{
  new CEditUpDown(NULL);
}
//---------------------------------------------------------------------------
__fastcall CEditUpDown::CEditUpDown(TComponent* Owner) : TCustomPanel(Owner)
{
  FLibelleFont  = 0;
  FSizeEsp      = 10;

  m_pfnOnChange = NULL;

  m_Edit                    = new CEdit(this);
  InsertControl(m_Edit);
  m_Edit->Align             = alClient;
  m_Edit->Alignment         = taRightJustify;
  m_Edit->Filtres           = m_Edit->Filtres << CEdit::feTotal;
  m_Edit->OnIllegalChar     = CEdit1IllegalChar;
  m_Edit->OnIllegalPaste    = CEdit1IllegalPaste;
  m_Edit->OnExit            = Edit1Exit;
  m_Edit->OnChange          = m_EditChange;

  m_UpDown              = new TUpDown(this);
  InsertControl(m_UpDown);
  m_UpDown->Align       = alRight;
  m_UpDown->Associate   = m_Edit;
  m_UpDown->Min         = 0;
  m_UpDown->Max         = 100;
  m_UpDown->OnClick     = m_UpDownClick;

  Height        = m_Edit->Height;
  Width         = 51;
  BevelInner    = bvNone;
  BevelOuter    = bvNone;

  FLibelleFont = new TFont();
  FLibelleFont->Assign(LibelleFont);
//  FLibelleFont->Assign(m_Edit->Font);
  FLibelleFont->OnChange = LibelleFontChange;

  LLibelle = new TLabel(this);
  LLibelle->Transparent = true;
  LLibelle->FocusControl = this;
}
//---------------------------------------------------------------------------
__fastcall CEditUpDown::~CEditUpDown()
{
  delete LLibelle;     LLibelle = NULL;
  delete m_Edit;       m_Edit = NULL;
  delete m_UpDown;     m_UpDown = NULL;
  delete FLibelleFont; FLibelleFont = NULL;
}
//---------------------------------------------------------------------------
int __fastcall CEditUpDown::GetMinMax(int index) const
{
  switch (index)
    {
      case 0:
        return m_UpDown->Min;
      case 1:
        return m_UpDown->Max;
      case 2:
        return m_UpDown->Position;
      default:
        ASSERT(false);

        return 0;
    }
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetMinMax(int index,int MinMax)
{
  switch (index)
    {
      case 0:
        if (m_UpDown->Position < MinMax)
          m_UpDown->Position = MinMax;
        m_UpDown->Min = MinMax;
        break;
      case 1:
        if (m_UpDown->Position > MinMax)
          m_UpDown->Position = MinMax;
        m_UpDown->Max = MinMax;
        break;
      case 2:
        m_UpDown->Position = MinMax;
        break;
      default:
        ASSERT(false);
    }
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::CEdit1IllegalChar(CCustomEdit *pedSender,WORD &cIllegalCharCode,bool &bAccept)
{
  bAccept = (cIllegalCharCode >= '0') && (cIllegalCharCode <= '9');
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::CEdit1IllegalPaste(CCustomEdit *pedSender,AnsiString &strClipText,bool &bAccept)
{
  for (int i = 1;i < strClipText.Length();i++)
    if ((strClipText[i] < '0') || (strClipText[i] > '9'))
      {
        bAccept = false;
        return;
      }
  bAccept = true;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::Edit1Exit(TObject *Sender)
{
  try
    {
      if (StrToInt(m_Edit->Text) != m_UpDown->Position)
        m_Edit->Text = m_UpDown->Position;
    }
  catch (...)
    {
      m_Edit->Text = m_UpDown->Position;
    }
}
//---------------------------------------------------------------------------
bool __fastcall CEditUpDown::GetEnabled()
{
  return m_Edit->Enabled;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetEnabled(bool enabled)
{
  m_Edit->Enabled = enabled;
  m_UpDown->Enabled = enabled;
  LLibelle->Enabled   = enabled;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetLibelle(const AnsiString libelle)
{
  FLibelle = libelle;
  LLibelle->Visible = Visible;
  LLibelle->Enabled = Enabled;
  LLibelle->Parent = Parent;
  LLibelle->Font = LibelleFont;
  LLibelle->Caption = FLibelle;
  LLibelle->Alignment = taRightJustify;
  ReplaceLibelle();
}
//---------------------------------------------------------------------------
TFont *__fastcall CEditUpDown::GetLibelleFont() const
{
//  return m_Edit->LibelleFont;
  return FLibelleFont;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetLibelleFont(TFont *Fonte)
{
  FLibelleFont->Assign(Fonte);
}
//---------------------------------------------------------------------------
bool __fastcall CEditUpDown::GetReadOnly() const
{
  return m_Edit->ReadOnly;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetReadOnly(bool Readonly)
{
  m_Edit->ReadOnly = Readonly;
  m_UpDown->Enabled = !Readonly;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetSizeEsp( int iSizeEsp )
{
  FSizeEsp = iSizeEsp;
  ReplaceLibelle();
}
//---------------------------------------------------------------------------
bool __fastcall CEditUpDown::GetVisible() const
{
  return TCustomPanel::Visible;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetVisible(bool visible)
{
  TCustomPanel::Visible = visible;
  LLibelle->Visible = visible;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::LibelleFontChange(TObject *Sender)
{
  LLibelle->Font->Assign(FLibelleFont);
  ReplaceLibelle();
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::WndProc(TMessage &Message)
{
	switch (Message.Msg)
		{
  		case WM_SETFOCUS:
	  		{
		    	if (!ComponentState.Contains(csDesigning))
    				{
		      		if (ReadOnly)
      					{
			        		Message.Msg = WM_NULL;
        					::SetFocus((HWND)Message.WParam);
      					}
			      	else
                if (m_Edit->AutoSelect)
        					m_Edit->SelectAll();
				    }
			    break;
  			}
	  	case WM_WINDOWPOSCHANGED:
  			{
	  			TCustomPanel::WndProc(Message);
          if (LLibelle)
            ReplaceLibelle();
				  Message.Msg = WM_NULL;
    			break;
		  	}
  		case WM_ENABLE:
	    	Enabled = (bool)Message.WParam;
  			break;
  		case WM_READONLY:
  			ReadOnly = (bool)Message.WParam;
	  		break;
		}
	TCustomPanel::WndProc(Message);
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::m_EditChange(TObject *Sender)
{
    if (m_pfnOnChange)
        m_pfnOnChange(Sender);
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::m_UpDownClick(TObject *Sender, TUDBtnType Button)
{
    if (m_pfnOnChange)
        m_pfnOnChange(Sender);
}
//---------------------------------------------------------------------------
bool __fastcall CEditUpDown::GetObligatoire( void)
{
  return m_Edit->Obligatoire;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetObligatoire( bool bObligatoire)
{
  m_Edit->Obligatoire = bObligatoire;
}
//---------------------------------------------------------------------------
bool __fastcall CEditUpDown::GetBleuSiHint( void)
{
  return m_Edit->BleuSiHint;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetBleuSiHint( bool bBleuSiHint)
{
  m_Edit->BleuSiHint = bBleuSiHint;
}
//---------------------------------------------------------------------------
TCorrectementRempliEvent __fastcall CEditUpDown::GetOnEstCorrectementRempli()
{
  return m_Edit->OnEstCorrectementRempli;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetOnEstCorrectementRempli( TCorrectementRempliEvent OnEstCorrectementRempli)
{
  m_Edit->OnEstCorrectementRempli = OnEstCorrectementRempli;
}
//---------------------------------------------------------------------------
bool __fastcall CEditUpDown::GetTabStop()
{
  return m_Edit->TabStop;
}
//---------------------------------------------------------------------------
void __fastcall CEditUpDown::SetTabStop(bool _bTabStop)
{
  m_Edit->TabStop = _bTabStop;
}
//---------------------------------------------------------------------------
namespace Ceditupdown
{
  void __fastcall PACKAGE Register()
  {
     TComponentClass classes[1] = {__classid(CEditUpDown)};
     RegisterComponents("EIC Ctrlstd", classes, 0);
  }
}
//---------------------------------------------------------------------------
